package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.model.user.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.Kernel;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 请求
 * 转发视图
 */
@Controller
@SessionAttributes(names = {"userInfoList"},types = {UserInfo.class})//指定哪些数据保存到Session中
public class HelloController {

    /**
     * 三大返回值 ：
     *  1、void
     *  2、ModelAndView
     *  3、String
     */

    //请深圳市
/*    @GetMapping("/hello")
    public void hello(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setAttribute("hello","同学们好");
        request.getRequestDispatcher("/hello.html").forward(request,response);
    }*/
/*    @GetMapping("/hello")
    public ModelAndView hello() throws Exception{
        ModelAndView mv = new ModelAndView();
        mv.addObject("hello","同学们好");
        mv.setViewName("hello");
        return mv;
    }*/
    @GetMapping("/hello")
    public String hello(Map map){
        map.put("hello","同学们好");
        List<UserInfo> userInfoList = new ArrayList<>();
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setId(1L);
        userInfo1.setName("小黑");
        userInfoList.add(userInfo1);
        UserInfo userInfo2 = new UserInfo();
        userInfo2.setId(2L);
        userInfo2.setName("小红");
        userInfoList.add(userInfo2);
        map.put("userInfoList",userInfoList);
        //宝强
        map.put("gname","<span style=\"color:green\">宝强</span>");

        return "hello";
    }
    @GetMapping("/hello2")
    public String hello2(@SessionAttribute(name = "userInfoList")
                                     List<UserInfo> userInfoList){

       // httpSession.getAttribute("userInfoList")
        System.out.println(userInfoList);

        return "hello";
    }
}
